﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlackBBS.Models.Data;
using System.IO;
using BlackBBS.Enums;

namespace BlackBBS.Menus
{
    public class LogOff : MenuBase
    {
        public LogOff()
        { 
        }

        public override void SendMenu()
        {
            this.Connection.SetCurrentArea(Area.LogOffMenu);
            
            var handled = false;
            while (!handled)
            {
                this.Connection.ClearScreen();
                this.Connection.SendFile("LOGOFF.ANS");
                handled = this.HandleResponse(this.Connection.SendScript("LOGOFF.MNU"));
            }
        }

        public bool HandleResponse(Dictionary<string, string> response)
        {
            if (response == null || response.Count == 0)
            {
                return false;
            }
            var key = (response.First().Key ?? string.Empty).ToLower();
            var value = (response.First().Value ?? string.Empty).ToLower();

            switch (value)
            {
                case "$": // send to timebank
                    return true;

                case "f": // send to feedback
                    return true;

                case "n": // send back to main menu
                    base.SendToMain();
                    return true;

                case "y": // log off 
                    this.DoLogOff();
                    return true;

            }
            return false;
        }

        void DoLogOff()
        {
            if (!this.DoImmediateDisconnect)
            {
                foreach (string file in Directory.GetFiles(this.Connection.ServerSettings.MenuFullPath(string.Empty), "GOODBYE*.ANS").OrderBy(x => x.ToString()))
                {
                    this.Connection.SendFile(file);
                }
            }
            this.Connection.Disconnect();
        }
    }
}
